<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/11
 * Time: 17:05
 */

namespace app\api\controller;


use app\api\Base2Controller;
use app\common\model\Goodsclass;
use think\Db;

/**
 * 服务端资料库
 * Class Ziliao
 * @package app\api\controller
 */
// extends BaseController
class Ziliao extends Base2Controller
{
    /**
     * 获取分类
     */
    public function type()
    {
        if (empty($typeid = intval(input('typeid')))){
            $result = model('goodsclass')->getOneClass('gc_id,gc_name,gc_images','gc_sort asc');
            $config = db('config')->where('code', 'qiniu_http')->find();
            foreach ($result as $key=>$value) {
                if ($value['gc_images'])
                {

                    $result[$key]['pic'] =$config['value'] . '/' . $value['gc_images'];
                }else{
                    $result[$key]['pic'] ='';
                }
//                $result[$key]['pic'] ='';
//                $pic_name = BASE_UPLOAD_PATH . '/' . ATTACH_COMMON . '/category-pic-' . $value['gc_id'] . '.jpg';
//                if (file_exists($pic_name)) {
//                    $result[$key]['pic'] = UPLOAD_SITE_URL . '/' . ATTACH_COMMON . '/category-pic-' . $value['gc_id'] . '.jpg';
//                }
            }
        }else {
            $result = model('goodsclass')->getTwoClass($typeid,'gc_id,gc_name','gc_sort asc');
        }

        return json_success_return($result);
    }

    /**
     * 获取三级分类
     */
    public function getTypes()
    {
        $model = model('goodsclass');
        $result = $model->getOneClass('gc_id,gc_name','gc_sort asc');
        foreach ($result as $key=>$value) {
            $result[$key]['child']=$result2 = $model->getTwoClass($value['gc_id'],'gc_id,gc_name','gc_sort asc');
            foreach ($result2 as $k=>$v)
            {
                $result[$key]['child'][$k]['child']=$model->getTwoClass($v['gc_id'],'gc_id,gc_name','gc_sort asc');
            }

        }
        return json_success_return($result);
    }

    /**
     * 教程列表
     */
    public function Ziliaolist()
    {
        if (!empty($typeid = intval(input('typeid'))))
        {
//            return json_fail_return('请正确输入参数');
            $where['cat_id1']=$typeid;
        }
        if ($name = input('search_name'))
        {
            $where['title']=  array('like', "%" .$name . "%");
        }
        $page = intval(input('page'))?intval(input('page')):1;
        $pagesize = intval(input('pagesize'))?intval(input('pagesize')):15;
        $offset = ($page-1)*$pagesize;
        $where['status']=0;
        $where['is_del']=0;
        #资料库-技术资料type 是 1或者3
        $where['type']= array(['=',1],['=',3],'or');
        $result = Db::name('course')
            ->where($where)
            ->order('id desc')->field('id,title,add_time,img,brand_name,like_num')
            ->limit($offset,$pagesize)
            ->select();
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        #like_num 收藏量  后台可以设置，如果等于0就输出count
        foreach ($result as $key=>$value){
            if ($value['img']) {
                $result[$key]['img'] = $file_http['value'].'/'.$value['img'];
            }
            if (!$value['like_num']){
                $ress = Db::name('course_user_collect')
                    ->where(['course_id'=>$value['id'],'type2'=>3])
                    ->field('count(*) as count')->find();
                $result[$key]['like_num']=$ress['count'];
            }
            if ($userid = $this->user_id)
            {
                $ress = Db::name('course_user_collect')
                    ->where(['course_id'=>$value['id'],'userid'=>$userid,'status'=>1,'type2'=>3])
                    ->field('count(*) as count')->find();
                $result[$key]['status_user_like']=$ress['count'];
            }
            $result[$key]['add_time']=date('Y-m-d');
        }
        return json_success_return($result);
    }

    /**
     * 教程详情
     */
    public function Ziliaodetail()
    {
        if (empty($detailid = intval(input('detailid'))))
        {
            return json_fail_return('请正确输入参数');
        }
        $qu = intval(input('son'))?intval(input('son')):0;
        $type = intval(input('type')); #2--教程 3--技术资料

        $where['id']=$detailid;
        $where['is_del']=0;

        if ($qu == 0) {
            $new_where['course_id'] = $detailid;
            $new_where['type2'] = $type; 
            $new_where['userid'] = $this->user_id; 

            $course_info = db('course_user_collect')->where($new_where)->find();

            #判断是否收藏
            if(!empty($course_info)){
                $detail = Db::name('course')->alias('c')
                    ->join('course_user_collect cuc','cuc.course_id=c.id')
                    ->where($new_where)
                    ->field('c.id,title,add_time,content,img,brand_name,gc_name,c.status,cuc.status as qu_status')
                    ->find();
                $course_id = $detail['id'];
            }else{
                $detail = Db::name('course')
                    ->where($where)
                    ->find();
                $detail['qu_status'] = 0;
                $course_id = $detail['id'];
            }
            if($detail['status'] == 1){
                return json_fail_return('当前资料库未启用');
            }
            
        }else{
            $detail = Db::name('course_info')
                ->where(['id'=>$detailid,'is_del'=>0])
                ->field('id,title,add_time,content,video,status,course_id')
                ->find();
            $detail['content']=html_entity_decode($detail['content']);
            $course_id =$detail['course_id'];
        }

        

        
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        if (isset($detail['img'])) {
            $detail['img'] = $file_http['value'].'/'.$detail['img'];
        }
        if (isset($detail['video'])) {
            $detail['video'] = $file_http['value'].'/'.$detail['video'];
        }
        $detail['add_time']=date('Y-m-d',$detail['add_time']);
        $res = Db::name('course_info')
            ->where(['course_id'=>$course_id,'status'=>1,'is_del'=>0])->field('id,title,qu')
            ->order('x_id asc')
            ->select();
        $detail['ziArr'] =$res;
        return json_success_return($detail);
    }
//
//    /**
//     * 子教程详情
//     */
//    public function ZiliaodtailSon()
//    {
//        if (empty($detailid = intval(input('detailid'))))
//        {
//            return json_fail_return('请正确输入参数');
//        }
//
//        $result = Db::name('course_info')
//            ->where(['id'=>$detailid,'is_del'=>0])
//            ->find();
//        if (!$result['status']){
//            return json_fail_return('当前资料库未启用');
//        }
//        $file_http = db('config')->where('code', 'qiniu_http')->find();
//        if ($result['video']) {
//            $result['video'] = $file_http['value'].'/'.$result['video'];
//        }
//        $result['add_time']=date('Y-m-d',$result['add_time']);
//        $result['content']=html_entity_decode($result['content']);
//        $res = Db::name('course_info')
//            ->where(['course_id'=>$result['course_id'],'status'=>1,'is_del'=>0])->field('id,title')
//            ->order('x_id desc')
//            ->select();
//        $result['ziArr'] =$res;
//        return json_success_return($result);
//    }

    /**
     * 收藏取消收藏
     */
    public function ZiliaoCollect()
    {
        // 2--培训 3--资料库
        if (empty($type2 = intval(input('type'))) || empty($id = intval(input('detailid'))) || empty($userid = $this->user_id))
        {
            return json_fail_return('请正确传入参数');
        }
        $status = intval(input('status'))?intval(input('status')):0;
        $type = 0;

        $where = array('course_id'=>$id,'userid'=>$userid,'type'=>$type);
        // $where['status'] = 1;
        $where['type2'] = $type2;
        $ress = Db::name('course_user_collect')
            ->where($where)->find();

        if(!empty($ress)){
            #如果有值
            if($ress['status'] == 1){
                #如果是收藏的话，改为未收藏
                $data = [
                    'type' => $type,
                    'status' => 0,
                    'updatetime'=>time()
                ];
                $res = Db::name('course_user_collect')->where($where)->update($data);
                if ($res) {
                    $like_num =  db('course')->where(['id'=>$id])->field('like_num')->find();
                    $like_num = intval($like_num['like_num'])-1;
                    db('course')->where(['id'=>$id])->update(['like_num'=>$like_num]);
                    return json_success_return([], '取消收藏成功');
                } else {
                    return json_fail_return('取消收藏失败');
                }

            }else{
                #如果是未收藏状态的话，改为已收藏
                $data = [
                    'type' => $type,
                    'status' => 1,
                    'updatetime'=>time()
                ];
                $res = Db::name('course_user_collect')->where($where)->update($data);
                if ($res) {
                    $like_num =  db('course')->where(['id'=>$id])->field('like_num')->find();
                    $like_num = intval($like_num['like_num'])+1;
                    db('course')->where(['id'=>$id])->update(['like_num'=>$like_num]);
                    return json_success_return([], '收藏成功');
                } else {
                    return json_fail_return('收藏失败');
                }

            }
        }else{
            #没有值的话，加数据
            $data = [
                    'course_id' => $id,
                    'userid' => $userid,
                    'type' => $type,
                    'type2'=>$type2,
                    'status' => 1,
                    'addtime'=>time(),
                    'updatetime'=>time()
                ];
                $res = Db::name('course_user_collect')->insertGetId($data);
                if ($res) {
                    if ($type){
//                       1子教程,查找出主教程id
                        $ziArr = db('course_info')->where(['id'=>$id])->find();
                        $id = $ziArr['course_id'];
                    }
                    $like_num =  db('course')->where(['id'=>$id])->field('like_num')->find();
                    $like_num = intval($like_num['like_num'])+1;
                    db('course')->where(['id'=>$id])->update(['like_num'=>$like_num]);
                    return json_success_return([], '收藏成功');
                } else {
                    return json_fail_return('收藏失败');
            }
        }    


//         if ($status) {
//             #添加收藏
//             if ($ress) {
//                 return json_fail_return('已收藏');
//             } else {
//                 $data = [
//                     'course_id' => $id,
//                     'userid' => $userid,
//                     'type' => $type,
//                     'type2'=>$type2,
//                     'status' => 1,
//                     'addtime'=>time(),
//                     'updatetime'=>time()
//                 ];
//                 $res = Db::name('course_user_collect')->insertGetId($data);
//                 if ($res) {
//                     if ($type){
// //                       1子教程,查找出主教程id
//                         $ziArr = db('course_info')->where(['id'=>$id])->find();
//                         $id = $ziArr['course_id'];
//                     }
//                     $like_num =  db('course')->where(['id'=>$id])->field('like_num')->find();
//                     $like_num = intval($like_num['like_num'])+1;
//                     db('course')->where(['id'=>$id])->update(['like_num'=>$like_num]);
//                     return json_success_return([], '收藏成功');
//                 } else {
//                     return json_fail_return('收藏失败');
//                 }
//             }
//         }else{
//             if ($ress){
//                 $data = [
//                     'type' => $type,
//                     'status' => 0,
//                     'updatetime'=>time()
//                 ];
//                 $res = Db::name('course_user_collect')->where($where)->update($data);
//                 if ($res) {
//                     $like_num =  db('course')->where(['id'=>$id])->field('like_num')->find();
//                     $like_num = intval($like_num['like_num'])-1;
//                     db('course')->where(['id'=>$id])->update(['like_num'=>$like_num]);
//                     return json_success_return([], '取消收藏成功');
//                 } else {
//                     return json_fail_return('取消收藏失败');
//                 }
//             }else{
//                 return json_fail_return('未收藏');
//             }
//         }
    }

    /**
     * 培训列表
     */
    public function PeixunList()
    {

        if (!empty($typeid = intval(input('typeid'))))
        {
            $where['cat_id1']=$typeid;
        }
        $sorts = intval(input('sort'))?intval(input('sort')):0;
        if ($sorts)
        {
            #热门列表
            $sort ='click_num desc';
        }else{
            #最新
            $sort='add_time desc';
        }
        if ($name = input('search_name'))
        {
            $where['title']=  array('like', "%" .$name . "%");
        }
        
        $page = intval(input('page'))?intval(input('page')):1;
        $pagesize = intval(input('pagesize'))?intval(input('pagesize')):15;
        $offset = ($page-1)*$pagesize;
        $where['status']=0;
        $where['is_del']=0;
        #培训-教程 是 1或者2
        $where['type']= array(['=',1],['=',2],'or');
        $result = Db::name('course')
            ->where($where)
            ->order($sort)->field('id,title,add_time,img,brand_name,like_num')
            ->limit($offset,$pagesize)
            ->select();
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        #like_num 收藏量  后台可以设置，如果等于0就输出count
        foreach ($result as $key=>$value){
            if ($value['img']) {
                $result[$key]['img'] = $file_http['value'].'/'.$value['img'];
            }
            if (!$value['like_num']){
                $ress = Db::name('course_user_collect')
                    ->where(['course_id'=>$value['id'],'type2'=>2])
                    ->field('count(*) as count')->find();
                $result[$key]['like_num']=$ress['count'];
            }
//            var_dump($this->user_id);
            if ($userid = $this->user_id)
            {
                $ress = Db::name('course_user_collect')
                    ->where(['course_id'=>$value['id'],'userid'=>$userid,'status'=>1,'type2'=>2])
                    ->field('count(*) as count')->find();
                $result[$key]['status_user_like']=$ress['count'];
            }
            $result[$key]['add_time']=date('Y-m-d');
        }
        return json_success_return($result);

    }

}
